﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using System.Data;
using Yipusoft.Exercitation.DBUtility;

public partial class Combat_SkillStat : System.Web.UI.Page
{
    string id = "55F5139F-489A-487D-8D55-FF6E555C7FDA";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            InitData();
    }

    private void InitData()
    {
        for (int i = 2010; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        ddlMonth.SelectedValue = DateTime.Now.Month.ToString();

        ddlStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        ddlStation.DataTextField = "itemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();

        ddlStation.Items.Insert(0, new ListItem("全部", "0000"));
        ddlRank.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();
        ddlRank.Items.Insert(0, new ListItem("全部", "0000"));

        ddlItem.DataSource = ItemSkill.GetList(" ParentID='" + id + "'");
        ddlItem.DataTextField = "ItemSkillName";
        ddlItem.DataValueField = "ID";
        ddlItem.DataBind();
        ddlItem.Items.Insert(0, new ListItem("全部", "0000"));
    }
    public string GetTaskID()
    {
        TaskBaseInfo taskInfo = TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + "").FirstOrDefault();
        return taskInfo != null ? taskInfo.TaskID.ToString() : Guid.Empty.ToString();
    }
    public string GetRankAndStation()
    {
        string where = string.Empty;
        if (!ddlRank.SelectedValue.Equals("0000"))
        {
            where = " And RankID='" + ddlRank.SelectedValue + "'";
        }
        if (!ddlStation.SelectedValue.Equals("0000"))
        {
            where += " And StationID='" + ddlStation.SelectedValue + "'";
        }

        return where;
    }

    private void Bind()
    {
        List<Yipusoft.Account.Model.DepartmentInfo> departmentList = Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000' And DepartmentID<>'1300000' And Left(DepartmentID,4)<>'1314' And Left(DepartmentID,4)<>'1315'");
        DataTable dtb = new DataTable();
        DataColumn clm;
        DataRow drw;
        clm = new DataColumn("DepartmentID", typeof(System.String));
        dtb.Columns.Add(clm);
        clm = new DataColumn("DepartmentName", typeof(System.String));
        dtb.Columns.Add(clm);
        clm = new DataColumn("InputScore", typeof(System.Decimal)); //录入率
        dtb.Columns.Add(clm);
        clm = new DataColumn("OrderScore", typeof(System.String)); //录入率
        dtb.Columns.Add(clm);

        foreach (Yipusoft.Account.Model.DepartmentInfo dpt in departmentList)
        {
            drw = dtb.NewRow();
            drw["DepartmentID"] = dpt.DepartmentID;
            drw["DepartmentName"] = dpt.ShortName;
            object[]array= GetScore(dpt.DepartmentID);
            drw["InputScore"] = array[0];
            drw["OrderScore"] = array[1];
            dtb.Rows.Add(drw);

        }
        UserChart.Series["技能达标率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "InputScore");
        for (int i = 0; i < dtb.Rows.Count; i++)
        {
           UserChart.Series["技能达标率"].Points[i].Href = "SkillStat2.aspx?DepartmentID=" + dtb.Rows[i]["DepartmentID"].ToString() + "&Year=" + ddlYear.SelectedValue + "&Month=" + ddlMonth.SelectedValue + "&RankID=" + ddlRank.SelectedValue + "&StationID=" + ddlStation.SelectedValue + "&ItemID=" + ddlItem.SelectedValue;
           UserChart.Series["技能达标率"].Points[i].ToolTip = dtb.Rows[i]["OrderScore"].ToString();
        }
    }

    public object[] GetScore(string DepartmentID)
    {
        object[] array = new object[2];

        string itemWhere = string.Empty;
        if (!ddlItem.SelectedValue.Equals("0000"))
            itemWhere = " And SkillID IN (Select SkillID From ItemSkill Where ParentID IN (Select ID From ItemSkill Where ParentID ='" + ddlItem.SelectedValue + "'))";

        string itemWhere2 = string.Empty;
        if(!string.IsNullOrEmpty(GetRankAndStation()))
        {
            itemWhere2 = " And SoldierID IN (Select SoldierID From TaskSoldiers Where 1=1 " + GetRankAndStation() + "  And Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "')";
        }

        //已录入且已达标的技能
        string sql = "Select Count(*) From TaskScoreView Where IsPass=1 And TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "'" + itemWhere + itemWhere2;
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql, null).ToString());

        //应录入的技能数量
        sql = "Select Count(*) From ( Select b.ID,a.DepartmentID,a.SoldierID,a.TaskID,a.RankID,a.StationID From TaskSoldiersView a  join TaskItems  b on a.RankID=b.RankID And a.StationID=b.StationID and a.TaskID=b.TaskID"
            + " And a.TaskID='" + GetTaskID() + "' ) as c  "
            + " join TaskSubject d on c.ID=d.TaskItemID  And  d.TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)= '" + DepartmentID.Substring(0, 4) + "'" + itemWhere + GetRankAndStation();
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql, null).ToString());

        if (count2 > 0)
            array[0]= (count / count2) * 100;
        else
            array[0]= 0;

        array[1] = "合格技能:" + count.ToString() + " 总技能:" + count2.ToString();
        return array;

    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        Bind();
    }
}
